<script setup lang="ts">
import {
  getProductPage,
  getProductForm,
  addUser,
  updateUser,
  deleteUsers,
} from "@/api/OutHouse/index";

import { UserPageVO, UserForm, UserQuery } from "@/api/OutHouse/types";

defineOptions({
  name: "OutHouse",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);
const UserFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<UserQuery>({
  pageindex: 1,
  pagesize: 10,
});

const roleList = ref<UserPageVO[]>();

const dialog = reactive({
  title: "",
  visible: false,
});

/**
 * 出库表单返回值
 */
const formData = reactive<UserForm>({});

const rules = reactive({
  // productName: [{ required: true, message: "请输入出库名称", trigger: "blur" }],
  // stockRemoveId: [{ required: true, message: "请输入出库编码", trigger: "blur" }],
  // price: [{ required: true, message: "请输入价格", trigger: "blur" }],
  // weight: [{ required: true, message: "请输入重量", trigger: "blur" }],
  // supplier: [{ required: true, message: "请输入供应商", trigger: "blur" }],
});

const menuDialogVisible = ref(false);

const menuList = ref<OptionType[]>([]);

interface CheckedRole {
  id?: number;
  name?: string;
}
let checkedRole: CheckedRole = reactive({});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getProductPage(queryParams)
    .then(({ data }) => {
      roleList.value = data.rows;
      total.value = data.totalRows;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageindex = 1;
  handleQuery();
}

/** 行checkbox 选中事件 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

/** 打开角色表单弹窗 */
function openDialog(stockRemoveId?: string) {
  dialog.visible = true;
  if (stockRemoveId) {
    dialog.title = "修改出库数据";
    getProductForm(stockRemoveId).then(({ data }) => {
      Object.assign(formData, data);
      console.log(data);
    });
  } else {
    dialog.title = "新增出库数据";
  }
}

/** 角色保存提交 */
function handleSubmit() {
  UserFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const roleId = formData.stockRemoveId;
      if (roleId) {
        updateUser(formData)
          .then(() => {
            ElMessage.success("修改成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        addUser(formData)
          .then(() => {
            ElMessage.success("新增成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  UserFormRef.value.resetFields();
  UserFormRef.value.clearValidate();

  formData.stockRemoveId = undefined;
}

/** 删除角色 */
function handleDelete(stockRemoveId?: string) {
  const roleIds = [stockRemoveId || ids.value].join(",");
  if (!roleIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    deleteUsers(roleIds)
      .then((ser: any) => {
        alert(roleIds);
        ElMessage.success(ser.message);
        resetQuery();
      })
      .finally(() => (loading.value = false));
  });
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="出库编号">
          <el-input
            v-model="queryParams.StockRemoveId"
            placeholder="出库编号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
          prop="keywords"
          label="类别"
          placeholder="请输入类别"
          style="width: 200px"
        >
          <el-select
            v-model="queryParams.stockRemoveType"
            clearable
            @keyup.enter="handleQuery"
          >
            <el-option :key="1" label="销售提货出货" :value="1" />
            <el-option :key="2" label="采购退货出库" :value="2" />
            <el-option :key="3" label=" 调拨出库" :value="3" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery()"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 删除新增，列表 -->
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success"
          ><i-ep-plus @click="openDialog()" />新增</el-button
        >
        <el-button
          type="danger"
          @click="handleDelete()"
          :disabled="ids.length === 0"
          ><i-ep-delete />删除</el-button
        >
      </template>

      <!-- 列表 -->
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="roleList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" prop="stockRemoveId" />
        <el-table-column label="出库类别" prop="stockRemoveType" />
        <el-table-column label="销售单号" prop="marketId" />
        <el-table-column label="客户姓名" prop="clientName" />
        <el-table-column label="联系方式" prop="phone" />
        <el-table-column label="制单人" prop="modelName" />
        <el-table-column label="仓库编号" prop="wareHouseId" />
        <el-table-column label="制单日期" prop="modelDate" />

        <el-table-column fixed="right" label="操作">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row.stockRemoveId)"
            >
              <i-ep-edit />编辑
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="handleDelete(scope.row.stockRemoveId)"
            >
              <i-ep-delete />删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageindex"
        v-model:limit="queryParams.pagesize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 出库表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="closeDialog"
    >
      <el-form
        ref="UserFormRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="出库编号" prop="productName">
              <el-input
                v-model="formData.stockRemoveId"
                placeholder="请输入出库编号"
              />
            </el-form-item>
            <el-form-item label="销售单号" prop="marketId">
              <el-input
                v-model="formData.marketId"
                placeholder="请输入销售单号"
              />
            </el-form-item>
            <el-form-item label="客户编号" prop="clientId">
              <el-input
                v-model="formData.clientId"
                placeholder="请输入客户编号"
              />
            </el-form-item>
            <el-form-item label="客户姓名" prop="clientName">
              <el-input
                v-model="formData.clientName"
                placeholder="请输入客户姓名"
              />
            </el-form-item>
            <el-form-item label="联系方式" prop="phone">
              <el-input v-model="formData.phone" placeholder="请输入联系方式" />
            </el-form-item>
            <el-form-item label="联系人" prop="linkman">
              <el-input v-model="formData.linkman" placeholder="请输入联系人" />
            </el-form-item>
            <el-form-item label="出库类别" prop="stockRemoveType">
              <el-select v-model="formData.stockRemoveType">
                <el-option :key="1" label="销售提货出货" :value="1" />
                <el-option :key="2" label="采购退货出库" :value="2" />
                <el-option :key="3" label="调拨出库" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="制单人" prop="modelName">
              <el-input
                v-model="formData.modelName"
                placeholder="请输入制单人"
              />
            </el-form-item>
            <el-form-item label="仓库编号" prop="wareHouseId">
              <el-input
                v-model="formData.wareHouseId"
                placeholder="请输入仓库编号"
              />
            </el-form-item>
            <el-form-item label="制单时间" prop="modelDate">
              <el-input
                v-model="formData.modelDate"
                placeholder="请输入制单时间"
              />
            </el-form-item>
            <el-form-item label="备注" prop="desc">
              <el-input v-model="formData.desc" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<!-- function defineOptions(arg0: { name: string; inheritAttrs: boolean; }) { throw
new Error("Function not implemented."); } -->
